package live.response;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import live.common.Format;
import live.common.Log4jXmlConfig;

import org.apache.log4j.Logger;




public class WebServicePay {
	private Logger log = Log4jXmlConfig.getInstance().returnLogger(WebServicePay.class);
	private int count=0;

	public String returnResult(String postUrl, Map<String, String> parameters) throws Exception
			{
		String postData = "";
		if (parameters != null && parameters.size() > 0) {
			List keys = new ArrayList(parameters.keySet());
			Collections.sort(keys);
			for (int i = 0; i < keys.size(); i++) {
				String key = (String) keys.get(i);
				Object value = parameters.get(key);
				if (i == keys.size() - 1) {
					postData += key + "=" + value;
				} else {
					postData += key + "=" + value + "&";
				}
			}
		}
		return publicMethod(postUrl, postData);
	}
	
	/**
	 * 过滤空值,封装传递参数
	 * @param postUrl
	 * @param parameters
	 * @return
	 */
	public String pixNull(String postUrl, Map<String, Object> parameters) throws Exception
	{
		String postData = "";
		if (parameters != null && parameters.size() > 0) {
			List keys = new ArrayList(parameters.keySet());
			Collections.sort(keys);
			for (int i = 0; i < keys.size(); i++) {
				Object key = (Object) keys.get(i);
				Object value = parameters.get(key);
					if(Format.getInstance().isNotNullOrEmpty(value)){
					if (i == keys.size() - 1) {
						postData += key + "=" + value;
					} else {
						postData += key + "=" + value + "&";
					}
				}
			}
		}
		return publicMethod(postUrl, postData);
	}
	
	private String publicMethod(String postUrl,String postData) throws Exception{
		HttpURLConnection httpURLConnection;
		StringBuilder sb=null;
		try {
			URL url = new URL(postUrl);
			httpURLConnection = (HttpURLConnection) url
					.openConnection();
			httpURLConnection.setDoOutput(true);
			httpURLConnection.setDoInput(true);
			httpURLConnection.setRequestMethod("POST");
			httpURLConnection.setConnectTimeout(10000);
			httpURLConnection.setReadTimeout(30000);
			httpURLConnection.connect();
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					httpURLConnection.getOutputStream(), "UTF-8"));
			out.write(postData);
			out.flush();
			out.close();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					(InputStream) httpURLConnection.getInputStream()));
			String line = null;
			sb = new StringBuilder();
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
			in.close();
			httpURLConnection.disconnect();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
	
}
